﻿using Model;
using Newtonsoft.Json.Linq;
using OrderingOnlineWeChat;
using PublicMethod;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OrderingOnlineWeChat.Controllers
{
    public class ShopController : BaseControl
    {
        DCXTService.Service1Client server2 = new DCXTService.Service1Client();

        //
        // GET: /Shop/
        #region View
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult MerInfo()
        {
            return View();
        }

        public ActionResult Index2(string shopnumber)
        {
            return View();
        }
        #endregion

        #region 获取商户所有信息，包括简介，配送时间，商品信息等等
        /// <summary>
        /// 获取商户所有信息，包括简介，配送时间，商品信息等等
        /// </summary>
        /// <param name="shopnumber"></param>
        /// <returns></returns>
        public ActionResult Shop(string shopnumber)
        {
            try
            {
                Model.orderinfo_Count mo = new Model.orderinfo_Count();
                string siteUrl = ConfigurationManager.AppSettings["Photo"];
                string result = server2.GetShopInfo(shopnumber, GetYZM._YZM, 1);//获取服务返回的数据
                JObject searcho = JObject.Parse(result);//将结果序列化
                string bs = searcho["BS"].ToString();
                if (bs!="1")
                {
                    return Content(result);
                }
                else
                {
                    Model.Order_ShopInfo shopinfo = obser<Model.Order_ShopInfo>.DataContractJsonDeserialize(searcho["rows"].ToString());//将json格式转为实体类
                                                                                                                                        //shopinfo.ShopInfo.MerPic = siteUrl + shopinfo.ShopInfo.MerPic;

                    shopinfo.ShopInfo.MerPic = shopinfo.ShopInfo.MerPic;
                    if (shopinfo.DishList != null)
                    {
                        for (int i = 0; i < shopinfo.DishList.Count; i++)
                        {
                            //shopinfo.DishList[i].DishesPic = siteUrl + shopinfo.DishList[i].DishesPic;
                            shopinfo.DishList[i].DishesPicSmall = shopinfo.DishList[i].DishesPicSmall;
                        }
                        mo.CountArr = shopinfo.CountArr;//ID
                        mo.DishList = shopinfo.DishList;//菜品列表
                        mo.PricList = shopinfo.PricList;
                        mo.ShopInfo = shopinfo.ShopInfo;
                        mo.TypeList = shopinfo.TypeList;

                        orderinfo oinfo = (orderinfo)Session[SessionStorage.OrderInfo];//将Session保存的实体转成可直接使用的实体
                        if (oinfo != null && oinfo.ShopInfo.MerID == shopnumber)
                        {
                            mo.DishesCount = oinfo.CountArr;
                            int A = mo.DishesCount.Length;
                        }
                        else
                        {
                            Session[SessionStorage.OrderInfo] = null;

                        }
                    }
                    //var json = new
                    //{
                    //    rows = shopinfo
                    //};
                    var json = new
                    {
                        rows = mo
                    };

                    return Json(json, JsonRequestBehavior.AllowGet);
                }

                
            }
            catch (Exception) { return null; }
        }
        #endregion

        #region 主要用于将订单信息保存至session，不要求返回任何数据
        /// <summary>
        /// 主要用于将订单信息保存至session，不要求返回任何数据，不做回调函数的处理
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public ActionResult saveOrderInfo(orderinfo info)
        {
            try
            {                
                Session[SessionStorage.OrderInfo] = info;//将实体类保存至Session
                return Content("1");
            }
            catch (Exception ex) { return Content(ex.ToString()); }
        }

        #endregion

        #region 根据商品号获取商品评价
        /// <summary>
        /// 根据商品号获取商品评价
        /// </summary>
        /// <param name="DishID"></param>
        /// <returns></returns>
        public ActionResult getDishEva(string DishID)
        {
            try
            {
                int star1 = 0, star2 = 0, star3 = 0, star4 = 0, star5 = 0;
                //string sDishID = DishID.ToString();
                string result = server2.GetDIshEva(DishID, GetYZM._YZM, 1);//获取服务返回的数据
                JObject searcho = JObject.Parse(result);//将结果序列化
                Model.DishInfo di = obser<Model.DishInfo>.DataContractJsonDeserialize(searcho["rows"].ToString());
                for (int i = 0; i < di.EvaList.Count; i++)
                {
                    di.EvaList[i].EvaTime = DateTimeHelper.ToDate2(di.EvaList[i].EvaTime);
                    di.EvaList[i].EvaTime = di.EvaList[i].EvaTime.Substring(0, 10);
                    di.EvaList[i].AccNum = di.EvaList[i].AccNum.Substring(0, 1) + "***" + di.EvaList[i].AccNum.Substring(di.EvaList[i].AccNum.Length - 1, 1);
                    switch (di.EvaList[i].EvaStars)
                    {
                        case 1:
                            star1++;
                            break;
                        case 2:
                            star2++;
                            break;
                        case 3:
                            star3++;
                            break;
                        case 4:
                            star4++;
                            break;
                        case 5:
                            star5++;
                            break;
                        default:
                            break;
                    }
                }
                //List<Model.View_Order_Eva> ls = new List<Model.View_Order_Eva>();
                //IList<JToken> jieguo = searcho["rows"].Children().ToList();
                //ls = ParseClass<Model.View_Order_Eva>.JsonList(jieguo); 
                var json = new
                {
                    rows = di,
                    total = di.EvaList.Count,
                    star1 = star1,
                    star2 = star2,
                    star3 = star3,
                    star4 = star4,
                    star5 = star5,

                };

                return Json(json, JsonRequestBehavior.AllowGet);
            }
            catch (Exception) { return null; }
        }
        #endregion

        public ActionResult cuow()
      {
          return Content("您还没有选中商品哦！");
      }
        public ActionResult aler()
        {
            return Content("该商户尚未营业，请过会儿再来！");
        }
    }
}
